package com.jsj.isdt.servlet;

import com.alibaba.fastjson.JSON;
import com.jsj.isdt.entity.Book;
import com.jsj.isdt.entity.Supplier;
import com.jsj.isdt.utils.DBUtils;
import com.jsj.isdt.utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ServletOrder extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String action =req.getParameter("action")==null ? "":req.getParameter("action");
        switch (action){
            case "reg":
                Reg(req,resp);
                break;
            case "reg1":
                Reg1(req,resp);
                break;
            default:
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
    private void  Reg(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        String id = req.getParameter("id");
        String bookname = req.getParameter("bookname");
        String username = req.getParameter("username");
        String number = req.getParameter("number");
        String price = req.getParameter("price");
        String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF8&useSSL=true&serverTimezone=UTC";
        String dbusername = "root";
        String dbpassword = "123456";
        //1.加载驱动
        try {PrintWriter out = resp.getWriter();
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, dbusername, dbpassword);
            String sql ="insert into bookorder(id,bookname,username,number,price) values (?,?,?,?,?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1,id);
            pst.setString(2,bookname);
            pst.setString(3,username);
            pst.setString(4,number);
            pst.setString(5,price);
            ResultData rd =new ResultData();
            if (pst.executeUpdate() >0){
                rd.setCode("200");
                rd.setMsg("成功");
            }
            else {  rd.setCode("501");
                rd.setMsg("失败");
            }
           out.write(JSON.toJSONString(rd));
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
    }
    private void  Reg1(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        String id = req.getParameter("id");
        String bookcase =  new String(req.getParameter("bookcase").getBytes("iso-8859-1"),"utf-8");
        String message =  new String(req.getParameter("message").getBytes("iso-8859-1"),"utf-8");
        String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF8&useSSL=true&serverTimezone=UTC";
        String dbusername = "root";
        String dbpassword = "123456";
        //1.加载驱动
        try {PrintWriter out = resp.getWriter();
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, dbusername, dbpassword);
            String sql ="insert into mes(id,bookcase,message) values (?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,id);
            ps.setString(2,bookcase);
            ps.setString(3,message);

            ResultData rd =new ResultData();
            if (ps.executeUpdate() >0){
                rd.setCode("200");
                rd.setMsg("成功");
            }
            else {  rd.setCode("501");
                rd.setMsg("失败");
            }
            out.write(JSON.toJSONString(rd));
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
    }
}
